﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace ThuySan.Models
{
    internal class DynamicViewDataDictionary : DynamicObject
    {
        private readonly Dictionary<string, object> _viewDataThunk;

        public DynamicViewDataDictionary()
        {
            _viewDataThunk = new Dictionary<string, object>();
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = _viewDataThunk.ContainsKey(binder.Name)
                             ? _viewDataThunk[binder.Name]
                             : null;
            // since ViewDataDictionary always returns a result even if the key does not exist, always return true
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _viewDataThunk[binder.Name] = value;
            // you can always set a key in the dictionary so return true
            return true;
        }

    }
}
